package com.tg.app.adapter;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tg.app.R;
import com.tg.data.http.entity.TimeZoneBean;

import java.util.List;

public class TimeZoneAdapter extends RecyclerView.Adapter<TimeZoneAdapter.TimeZoneViewHolder> {

    Context mContext;
    List<TimeZoneBean> mData;


    public interface OnItemClickListener{
         void onClick(int positon);
    }

    OnItemClickListener mOnItemClickListener;

    public TimeZoneAdapter(Context context,List<TimeZoneBean> data){
        this.mContext=context;
        this.mData=data;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener=onItemClickListener;
    }
    @NonNull
    @Override
    public TimeZoneViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.layout_timezone,null);
        return new TimeZoneViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TimeZoneViewHolder holder, final int position) {
        TimeZoneBean bean=mData.get(position);
        holder.zonetv.setText(bean.getZone());
        holder.timetv.setText(bean.getTime());
        if (bean.getSelected()==1){
            holder.choseiv.setVisibility(View.VISIBLE);
        }else {
            holder.choseiv.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener!=null){
                    mOnItemClickListener.onClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData==null?0:mData.size();
    }

    class TimeZoneViewHolder extends RecyclerView.ViewHolder{

        TextView zonetv;
        TextView timetv;
        ImageView choseiv;
        public TimeZoneViewHolder(View itemView) {
            super(itemView);
            zonetv=itemView.findViewById(R.id.zonetv);
            timetv=itemView.findViewById(R.id.timetv);
            choseiv=itemView.findViewById(R.id.choseiv);
        }
    }
}
